/**
 * Definition for singly-linked list.
 */
 function ListNode(val, next) {
   this.val = (val===undefined ? 0 : val)
     this.next = (next===undefined ? null : next)
     }
     
const a1 = new ListNode(1);
const a2 = new ListNode(2);
const a3 = new ListNode(3);
const a4 = new ListNode(4);
const a5 = new ListNode(5);
a1.next=a2;
a2.next=a3;
a3.next=a4;
a4.next=a5;
console.log(a1);

/**
 * @func 反转链表
 * @param {ListNode} head
 * @return {ListNode}
 */
 var reverseList = function(head) {
    if(!head || !head.next )  return head;
    let center = head;
    let left = null;
    while(center!=null){
        let right= center.next;
        center.next=left;
        left = center;
        center = right;
    }
    return left;
};

console.log(reverseList(a1));